package org.jeecg.modules.online.cgform.controller;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.io.File;
import java.lang.invoke.SerializedLambda;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FilenameUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.exception.TuringBootException;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.FileDownloadUtils;
import org.jeecg.common.util.SqlInjectionUtil;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.online.cgform.entity.OnlCgformField;
import org.jeecg.modules.online.cgform.entity.OnlCgformHead;
import org.jeecg.modules.online.cgform.service.IOnlCgformFieldService;
import org.jeecg.modules.online.cgform.service.IOnlCgformHeadService;
import org.jeecg.modules.online.cgform.utils.CgformUtil;
import org.jeecg.modules.online.config.exception.DBException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping({"/online/cgform/field"})
@RestController("onlCgformFieldController")
/* renamed from: org.jeecg.modules.online.cgform.c.c */
/* loaded from: hibernate-re-3.7.1-RC.jar:org/jeecg/modules/online/cgform/c/c.class */
public class OnlCgformFieldController {

    /* renamed from: a */
    private static final Logger f148a = LoggerFactory.getLogger(OnlCgformFieldController.class);
    @Autowired
    private IOnlCgformHeadService onlCgformHeadService;
    @Autowired
    private IOnlCgformFieldService onlCgformFieldService;
    @Value("${jeecg.path.upload}")
    private String uploadpath;

    /* renamed from: a */
//    private static /* synthetic */ Object m450a(SerializedLambda serializedLambda) {
//        String implMethodName = serializedLambda.getImplMethodName();
//        boolean z = true;
//        switch (implMethodName.hashCode()) {
//            case -1998322493:
//                if (implMethodName.equals("getTableName")) {
//                    z = false;
//                    break;
//                }
//                break;
//        }
//        switch (z) {
//            case false:
//                if (serializedLambda.getImplMethodKind() == 5 && serializedLambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && serializedLambda.getFunctionalInterfaceMethodName().equals("apply") && serializedLambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && serializedLambda.getImplClass().equals("org/jeecg/modules/online/cgform/entity/OnlCgformHead") && serializedLambda.getImplMethodSignature().equals("()Ljava/lang/String;")) {
//                    return (v0) -> {
//                        return v0.getTableName();
//                    };
//                }
//                break;
//        }
//        throw new IllegalArgumentException("Invalid lambda deserialization");
//    }

    @GetMapping({"/listByHeadCode"})
    /* renamed from: a */
    public Result<?> m454a(@RequestParam("headCode") String str) {
    	LambdaQueryWrapper<OnlCgformHead> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq((v0) -> {
            return v0.getTableName();
        }, str);
        OnlCgformHead onlCgformHead = this.onlCgformHeadService.getOne(lambdaQueryWrapper);
        if (onlCgformHead == null) {
            return Result.error("表名[" + str + "]不存在！");
        }
        return m447b(onlCgformHead.getId());
    }

    @GetMapping({"/listByHeadId"})
    /* renamed from: b */
    public Result<?> m447b(@RequestParam("headId") String str) {
    	QueryWrapper<OnlCgformField> queryWrapper = new QueryWrapper();
        queryWrapper.eq("cgform_head_id", str);
        queryWrapper.orderByAsc("order_num");
        return Result.ok(this.onlCgformFieldService.list(queryWrapper));
    }

    @GetMapping({"/list"})
    /* renamed from: a */
    public Result<IPage<OnlCgformField>> m448a(OnlCgformField onlCgformField, @RequestParam(name = "pageNo", defaultValue = "1") Integer num, @RequestParam(name = "pageSize", defaultValue = "10") Integer num2, HttpServletRequest httpServletRequest) {
        Result<IPage<OnlCgformField>> result = new Result<>();
        Wrapper initQueryWrapper = QueryGenerator.initQueryWrapper(onlCgformField, httpServletRequest.getParameterMap());
        IPage page = this.onlCgformFieldService.page(new Page(num.intValue(), num2.intValue()), initQueryWrapper);
        result.setSuccess(true);
        result.setResult(page);
        return result;
    }

    @PostMapping({"/add"})
    /* renamed from: a */
    public Result<OnlCgformField> m449a(@RequestBody OnlCgformField onlCgformField) {
        Result<OnlCgformField> result = new Result<>();
        try {
            this.onlCgformFieldService.save(onlCgformField);
            result.success("添加成功！");
        } catch (Exception e) {
            f148a.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    @PutMapping({"/edit"})
    /* renamed from: b */
    public Result<OnlCgformField> m446b(@RequestBody OnlCgformField onlCgformField) {
        Result<OnlCgformField> result = new Result<>();
        if (((OnlCgformField) this.onlCgformFieldService.getById(onlCgformField.getId())) == null) {
            result.error500("未找到对应实体");
        } else if (this.onlCgformFieldService.updateById(onlCgformField)) {
            result.success("修改成功!");
        }
        return result;
    }

    @DeleteMapping({"/delete"})
    /* renamed from: c */
    public Result<OnlCgformField> m445c(@RequestParam(name = "id", required = true) String str) {
        Result<OnlCgformField> result = new Result<>();
        if (((OnlCgformField) this.onlCgformFieldService.getById(str)) == null) {
            result.error500("未找到对应实体");
        } else if (this.onlCgformFieldService.removeById(str)) {
            result.success("删除成功!");
        }
        return result;
    }

    @DeleteMapping({"/deleteBatch"})
    /* renamed from: d */
    public Result<OnlCgformField> m444d(@RequestParam(name = "ids", required = true) String str) {
        Result<OnlCgformField> result = new Result<>();
        if (str == null || "".equals(str.trim())) {
            result.error500("参数不识别！");
        } else {
            this.onlCgformFieldService.removeByIds(Arrays.asList(str.split(CgformUtil.f233E)));
            result.success("删除成功!");
        }
        return result;
    }

    @GetMapping({"/queryById"})
    /* renamed from: e */
    public Result<OnlCgformField> m443e(@RequestParam(name = "id", required = true) String str) {
        Result<OnlCgformField> result = new Result<>();
        OnlCgformField onlCgformField = (OnlCgformField) this.onlCgformFieldService.getById(str);
        if (onlCgformField == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(onlCgformField);
            result.setSuccess(true);
        }
        return result;
    }

    @AutoLog(operateType = 1, value = "online表单批量单字段下载文件")
    @GetMapping({"/download/{code}/{id}/{field}"})
    /* renamed from: a */
    public void m452a(@PathVariable("code") String str, @PathVariable("id") String str2, @PathVariable("field") String str3, HttpServletResponse httpServletResponse) {
        try {
            f148a.info("[批量下载文件]开始批量下载文件:code=" + str + ",id=" + str2 + ",field=" + str3);
            SqlInjectionUtil.filterContent(str2, CgformUtil.f231C);
            OnlCgformHead table = this.onlCgformHeadService.getTable(str);
            List<OnlCgformField> queryFormFields = this.onlCgformFieldService.queryFormFields(table.getId(), true);
            if (queryFormFields == null || queryFormFields.isEmpty()) {
                throw new DBException("找不到字段，请确认配置是否正确!");
            }
            List<OnlCgformField> list = (List) queryFormFields.stream().filter(onlCgformField -> {
                return onlCgformField.getDbFieldName().equals(str3);
            }).collect(Collectors.toList());
            if (list.isEmpty()) {
                throw new DBException("找不到字段!");
            }
            String m360a = CgformUtil.m360a(this.onlCgformFieldService.queryFormData(list, CgformUtil.m317f(table.getTableName()), str2), str3);
            if (null == m360a || m360a.isEmpty()) {
                return;
            }
            List<String> arrayList = new ArrayList();
            if (m360a.contains(CgformUtil.f233E)) {
                arrayList.addAll(Arrays.asList(m360a.split(CgformUtil.f233E)));
            } else {
                arrayList.add(m360a);
            }
            String str4 = this.uploadpath + File.separator + "tmp" + File.separator + UUIDGenerator.generate() + File.separator;
            FileDownloadUtils.downloadFileMulti(httpServletResponse, (List) arrayList.stream().map(str5 -> {
            	Pattern pattern = Pattern.compile("^(http|https)://.*");
                Matcher match = pattern.matcher(str5);
                if (match.matches()) {
                    if (str5.contains("?")) {
                        str5 = str5.substring(0, str5.indexOf("?"));
                    }
                    return FileDownloadUtils.download2DiskFromNet(str5, str4 + FilenameUtils.getName(str5));
                }
                return this.uploadpath + File.separator + str5;
            }).collect(Collectors.toList()), list.get(0).getDbFieldTxt() + "_" + str2);
            new Thread(() -> {
                try {
                    Thread.sleep(10000L);
                    FileUtil.del(str4);
                } catch (InterruptedException e) {
                    f148a.error(e.getMessage(), e);
                }
            }).start();
        } catch (Exception e) {
            f148a.error("online表单批量单字段下载文件：" + e.getMessage(), e);
            throw new TuringBootException(e);
        }
    }
}
